package com.example.video.controller;

import com.example.video.common.Result;
import com.example.video.entity.VideoLog;
import com.example.video.service.IVideoLogService;
import com.example.video.util.TimeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author qiusuyang
 * @since 2024-04-13
 */
@Slf4j
@Controller
@RequestMapping("/videoLog")
public class VideoLogController {

    @Resource
    private IVideoLogService logService;

    @ResponseBody
    @PostMapping("/get/List")
    public Result<String> getList(@RequestParam Integer videoId){
        Result<String> rs = new Result<>();
        List<VideoLog> list = logService.getListByVideoId(videoId);
        String message = list.stream()
                .map(log->"错误发生时间： "+ TimeUtil.DateToStr(log.getUpData())
                        +"\n错误信息："+ log.getMessage()).reduce((a, b) -> a + "\n\n" + b).orElse("");
        String formattedMessage = message.replace("\n", "<br>");
        rs.setData(formattedMessage);
        rs.setCode(0);
        rs.setCount(0);
        rs.setMsg("获取成功！");
        return rs;
    }

    @ResponseBody
    @PostMapping("/get/List/device")
    public Result<String> getListByDeviceId(@RequestParam String deviceId){
        Result<String> rs = new Result<>();
        List<VideoLog> list = logService.getListByDeviceId(deviceId);
        String message = list.stream()
                .map(log->"错误发生时间： "+ TimeUtil.DateToStr(log.getUpData())
                        +"\n错误信息："+ log.getMessage()).reduce((a, b) -> a + "\n\n" + b).orElse("");
        String formattedMessage = message.replace("\n", "<br>");

        rs.setData(formattedMessage);
        rs.setCode(0);
        rs.setCount(0);
        rs.setMsg("获取成功！");
        return rs;
    }


    /**
     * @param item 修改或添加的信息
     * @return 是否成功修改或添加
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Boolean> saveItem(VideoLog item) {
        Result<Boolean> rs = new Result<>();

        Boolean flag = logService.save(item);
        rs.setData(flag);
        if (flag) {
            rs.setCode(200);
            rs.setMsg("成功");
        } else {
            rs.setCode(500);
            rs.setMsg("失败");
        }
        return rs;
    }

    /**
     * @param id 要删除数据的id
     * @return 是否删除成功
     */
    @ResponseBody
    @PostMapping("/delete")
    public Result<Boolean> deleteItem(Integer id) {
        Result<Boolean> rs = new Result<>();
        Boolean flag = logService.delete(id);
        rs.setData(flag);
        if (flag) {
            rs.setCode(200);
            rs.setMsg("成功");
        } else {
            rs.setCode(500);
            rs.setMsg("失败");
        }
        return rs;
    }

}
